package com.example.yis.adriodversioncode;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.AlteredCharSequence;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import com.example.yis.config.Global;

import static com.example.yis.config.Global.localVersion;
import static com.example.yis.config.Global.serverVersion;

public class VersionTest extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_version_test);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        initGlobal();
        CheckVersion();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_version_test, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * 初始化全局变量
     */
    public void initGlobal() {
        try {
            localVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
            serverVersion = 1;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void CheckVersion() {
        if (Global.localVersion ==Global.serverVersion) {
            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("软件升级").setMessage("发现新版本,建议立即更新使用.")
                    .setPositiveButton("更新", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //开启更新服务UpdateService
                            //这里为了把update更好模块化，可以传一些updateService依赖的值
                            //如布局ID，资源ID，动态获取的标题,这里以app_name为例
                            Intent updateIntent=new Intent(VersionTest.this,UpdateService.class);
                            updateIntent.putExtra("titleId",R.string.app_name);
                            startService(updateIntent);
                        }
                    }).setNegativeButton("取消",new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            alert.create().show();
        }else{
            //清理工作，略去
            //cheanUpdateFile(),文章后面我会附上代码

        }
    }
}
